package com.yeyks.common.base;

import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @author fuchangshuai
 * @date 2019/07/03 15:10
 */
@Data
@NoArgsConstructor
public class PageResult<T> {

    /**
     * 总个数
     */
    private long total;

    /**
     * 总页数
     */
    private Integer pageCount;

    /**
     * 是否有下一页
     */
    private Boolean hasNextPage;

    /**
     * 下一页页码
     */
    private Integer nextPage;

    /**
     * 列表
     */
    private List<T> list;

    public PageResult(long total, Integer pageCount, Boolean hasNextPage, Integer nextPage, List<T> list) {
        this.total = total;
        this.pageCount = pageCount;
        this.hasNextPage = hasNextPage;
        this.nextPage = nextPage;
        this.list = list;
    }

    public static <T> PageResult<T> getPageResult(List<T> list) {
        PageResult<T> result = new PageResult<>();
        PageInfo<T> pageInfo = PageInfo.of(list);
        result.setTotal(pageInfo.getTotal());
        result.setPageCount(pageInfo.getPages());
        result.setHasNextPage(pageInfo.isHasNextPage());
        result.setNextPage(pageInfo.getNextPage());
        result.setList(pageInfo.getList());
        return result;
    }
}
